﻿using FirstFloor.ModernUI.Presentation;
using FirstFloor.ModernUI.Windows.Controls;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Microsoft.Practices.Unity;
using appCtx = EasyInvoice.ApplicationContext.AppContext;

namespace EasyInvoice.WPF.ViewModel
{
    public class ClientListNewViewModel : NotifyPropertyChanged
    {
        private Services.ClientsService _serviceClients;
        
        #region Client List
        
        private ObservableCollection<ClientViewModel> _clients = new ObservableCollection<ClientViewModel>();

        public ObservableCollection<ClientViewModel> Clients
        { 
        
            get
            {
                this._clients = GetClients();
                return this._clients;
            }
            set
            {
                this._clients = value;
                this.OnPropertyChanged("Clients");
            }
        }

        public ClientViewModel SelectedClient
        { get; set; }
        #endregion

        #region Filter List
        
        private string _name = string.Empty;

        private bool? _CanIssueInvoices;

        public string Name
        {
            get
            {
                return this._name;
            }
            set
            {
                this._name = value;
                this.OnPropertyChanged("Name");
            }
        }

        public bool? CanIssueInvoices
        {
            get
            {
                return this._CanIssueInvoices;
            }
            set
            {
                this._CanIssueInvoices = value;
                OnPropertyChanged("CanIssueInvoices");
            }
        }

        #endregion

        private ObservableCollection<ClientViewModel> GetClients()
        {
            var returnValue = new ObservableCollection<ClientViewModel>();
            var clients = this._serviceClients.GetAll();

            clients = from Model.Client c in clients
                      where c.Name.ToUpperInvariant().Contains(_name.ToUpperInvariant())
                      select c;

            if (this._CanIssueInvoices.HasValue)
                clients = from Model.Client c in clients
                          where c.IsMaster == this._CanIssueInvoices
                          select c;
            clients.ToList().ForEach(client => returnValue.Add(new ClientViewModel(client)));
            return returnValue;
        }

        public ClientListNewViewModel()
        {
            _serviceClients = appCtx.Instance.IoC.Resolve<Services.ClientsService>();
        }


        #region Commands

        public bool CanAddClient
        {
            get{
                return ((UserViewModel)Properties.Settings.Default.AuthenticatedUser)!=null?
                    ((UserViewModel)Properties.Settings.Default.AuthenticatedUser).AddClients:false;
            }
        }

        public bool CanDeleteClient
        {
            get
            {
                return ((UserViewModel)Properties.Settings.Default.AuthenticatedUser) != null ?
                    ((UserViewModel)Properties.Settings.Default.AuthenticatedUser).DeleteClients : false;
            }
        }

        public bool CanEditClient
        {
            get
            {
                return !(((UserViewModel)Properties.Settings.Default.AuthenticatedUser) != null ?
                    ((UserViewModel)Properties.Settings.Default.AuthenticatedUser).EditClients : false);
            }
        }

        private ICommand showSaveCommand;

        public ICommand ShowSaveCommand
        {
            get
            {
                if (showSaveCommand == null)
                {
                    showSaveCommand = new Common.CommandBase(
                        i => this.SaveClient(),
                        i => ((UserViewModel)Properties.Settings.Default.AuthenticatedUser) != null ?
                            ((UserViewModel)Properties.Settings.Default.AuthenticatedUser).EditClients : false);
                }
                return showSaveCommand;
            }
        }

        private void SaveClient()
        {
            try
            {
                //this._clients.Where(client => client.Id == 0).ToList().ForEach(client => client.ShowSaveCommand.Execute(null));
                SelectedClient.ShowSaveCommand.Execute(null);
                this.OnPropertyChanged("Clients");
                this.OnPropertyChanged("SelectedClient");
            }
            catch (Exception ex)
            { ModernDialog.ShowMessage(ex.Message, "Save Client Failed", MessageBoxButton.OK); }
        }


        //for opening up the Delete Client window
        private ICommand showDeleteCommand;

        public ICommand ShowDeleteCommand
        {
            get
            {
                if (showDeleteCommand == null)
                {
                    showDeleteCommand = new Common.CommandBase(
                        i => this.DeleteClient(),
                        i => this.CanDelete());
                }
                return showDeleteCommand;
            }
        }

        private void DeleteClient()
        {
            var result = ModernDialog.ShowMessage(string.Format("Do you want to delete client {0} ?", SelectedClient.Name), "Delete Client Dialog", MessageBoxButton.YesNo);
            if (result == MessageBoxResult.Yes)
            {
                try
                {
                    SelectedClient.Delete();
                }
                catch (Exception ex)
                {
                    ModernDialog.ShowMessage(ex.Message, "Delete Client Failed", MessageBoxButton.OK);
                }
            }
            this.OnPropertyChanged("Clients");
            this.OnPropertyChanged("SelectedClient");

        }

        private bool CanDelete()
        {
            if (object.Equals((UserViewModel)Properties.Settings.Default.AuthenticatedUser, null)) return false;
            if (object.Equals(SelectedClient, null)) return false;
            return ((UserViewModel)Properties.Settings.Default.AuthenticatedUser).DeleteClients;
        }

        private ICommand refreshCommand;

        public ICommand RefreshCommand
        {
            get
            {
                if (refreshCommand == null)
                {
                    refreshCommand = new Common.CommandBase(i => this.RefreshClients(), null);
                }
                return refreshCommand;
            }
        }

        private void RefreshClients()
        {
            //save client
            this.OnPropertyChanged("Clients");
        }


    //    private ICommand showBrowseCommand;

    //    public ICommand ShowBrowseCommand
    //    {
    //        get
    //        {
    //            if (showBrowseCommand == null)
    //            {
    //                showBrowseCommand = new Common.CommandBase(i => this.ShowBrowse(), i=>true);
    //            }
    //            return showBrowseCommand;
    //        }
    //    }

    //    private void ShowBrowse()
    //    {
    //        new Common.BrowseFiles()
    //        {
    //            Title = "Browse for Images",
    //            Description = "",
    //        }.ShowDialog();
    //        //save client
    //        //this.OnPropertyChanged("Clients");
        //    }
        #endregion
    }
}
